package com.henrich.ccb.applet;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.henrich.ccb.utils.WeekDay;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * <p>功能描述：早报生成器 </p>
 * <p>修订记录</p>
 *
 * @Author xianghui.zh
 * @Version 1.0 2022/2/17
 **/
public class NewsGenerator {

    public static void main(String[] args) throws Exception {
        String weather = getWeather();
        File file = FileUtil.touch("dddd.txt");
        FileWriter writer = new FileWriter(file);
        writer.write(getDate() + "\n");
        writer.append(weather + "\n\n");
        for (int i = 0; i < 10; i++) {
            writer.append((i + 1) + ". " + "\n");
        }


    }
    //

    /**
     * 获取日期
     *
     * @return 2022-02-17/一月十七/星期四
     */
    public static String getDate() {
        LocalDate now = LocalDate.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = fmt.format(now);
        ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate(date));
        StringBuilder sb = new StringBuilder();
        sb.append(date);
        sb.append("/");
        sb.append(chineseDate.getChineseMonth() + chineseDate.getChineseDay());
        sb.append("/");
        sb.append(WeekDay.getCNByEA(now.getDayOfWeek().toString()));
        return sb.toString();
    }

    public static String getWeather() throws Exception {
        String ret = "";
        String url = "https://devapi.qweather.com/v7/weather/now?location=101020100&key=1783a49bc43b4eac97b750a8842d515a";
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        HttpGet httpGet = new HttpGet(url );
        // 响应模型
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            //使用工具类EntityUtils，从响应中取出实体表示的内容并转换成字符串
            ret = EntityUtils.toString(entity, "utf-8");
        }
        //5.关闭资源
        response.close();
        httpClient.close();
        JSONObject jsonObject = JSON.parseObject(JSON.parseObject(ret).get("now").toString());
        System.out.println(jsonObject.get("feelsLike"));
        StringBuilder sb = new StringBuilder();
        sb.append("天气"+jsonObject.get("text")+"/");
        sb.append("体感温度"+jsonObject.get("feelsLike")+"/");
        sb.append("风力"+jsonObject.get("windScale"));
        return sb.toString();
    }
}
